package com.peanut.controller;

import com.mvc.annotation.*;
import com.orm.annotation.Param;
import com.peanut.entity.ChannelType;
import com.peanut.entity.JsonResult;
import com.peanut.entity.vo.ChannelTypeVO;
import com.peanut.service.ChannelTypeService;
import com.spring.annotation.Autowired;
import com.spring.annotation.Controller;

import java.util.List;

@Controller
public class ChannelTypeController {
	@Autowired
	private ChannelTypeService service;
	
	
	@ResponseBody
	@RequestMapping("/channelType/query.do")
	public JsonResult queryChannelType(){
		List<ChannelType> channelTypes = service.queryAllChannelType();
		return JsonResult.success(channelTypes);
	}
	
	// 级联方式查询 ,被注释掉了
	// @ResponseBody
	// @RequestMapping("/channelType/query2.do")
	// public JsonResult queryChannelType2(){
	// 	List<ChannelTypeVO> channelTypeVOS = service.queryChannelType();
	// 	return JsonResult.success(channelTypeVOS);
	// }
	
	
	@ResponseBody
	@RequestMapping("/channelType/update.do")
	public JsonResult updateChannelType(@RequestBody @Valid ChannelType channelType){
		if(channelType.getId() == null){
			if (service.add(channelType)) {
				return JsonResult.success();
			}
		}else{
			if (service.updateById(channelType)) {
				return JsonResult.success();
			}
		}
		return JsonResult.fail();
	}
	
	@ResponseBody
	@RequestMapping("/channelType/delete.do")
	public JsonResult deleteChannelType(@RequestParam(required = true) int id){
		if (service.delete(id)) {
			return JsonResult.success();
		}
		return JsonResult.fail("删除失败,存在关联子类型或关联渠道");
	}
}
